export function getUrlArgOrDefault(argName, defultVal) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (let i = 0; i < vars.length; i++) {
        let kv = vars[i];
        let equal = kv.indexOf('=');
        let key = kv.substr(0, equal);
        let value = kv.substr(equal + 1);
        if (key === argName) {
            return decodeURI(value);
        }
    }

    console.log(`use ${defultVal} instead of ${argName} url argument in  getUrlArgOrDefault function `)
    return defultVal; //come from code rather than url
}


export function download(content, filename, type = 'text/json', vmDownload) {
    // 创建隐藏的可下载链接
    var eleLink = document.createElement('a');

    if (!('download' in eleLink)) {
        if (vmDownload) vmDownload.$message({
            showClose: true,
            type: "error",
            message: 'this browser donot support client download.'
        });
        console.error('this browser donot support client download.');
        return false;
    };

    eleLink.download = filename;
    eleLink.style.display = 'none';
    // 字符内容转变成blob地址
    var blob = new Blob([content], {
        type: type
    });
    eleLink.href = URL.createObjectURL(blob);
    console.log('download in browser\n', eleLink.outerHTML)
    // 触发点击
    document.body.appendChild(eleLink);
    eleLink.click();
    // 然后移除
    document.body.removeChild(eleLink);
};

export function readAsText(file, callback) {
    let reader = new FileReader();
    reader.onload = function() {
        if (callback) callback(this.result)
    };
    reader.readAsText(file);
};
